<template>
  <div>
    <n-card>
      <div
        style="
          margin-bottom: 10px;
          margin-bottom: 10px;
          display: flex;
          align-items: center;
          gap: 10px;
        "
      >
        <n-button type="primary" dashed size="small" @click="openDialog">
          <template #icon>
            <n-icon>
              <AddCircleOutline />
            </n-icon>
          </template>
          新增
        </n-button>
        <n-input placeholder="搜索" style="width: 200px" size="small">
          <template #prefix>
            <n-icon :component="SearchOutline" />
          </template>
        </n-input>
        <n-button
          :loading="loading"
          type="success"
          @click="handleClick"
          size="small"
        >
          <template #icon>
            <n-icon size="small">
              <SearchOutline />
            </n-icon>
          </template>
          查询
        </n-button>
      </div>
      <!-- <n-skeleton text :repeat="6" /> -->
      <n-spin :show="loading">
        <n-data-table
          :columns="columns"
          :data="tagData"
          :row-key="rowKey"
          default-expand-all
          style="margin-bottom: 10px"
          :max-height="tableHeight"
          :scroll-x="900"
        />

        <div style="display: flex; justify-content: flex-end">
          <n-pagination
            v-model:page="page"
            v-model:page-size="limit"
            :page-count="100"
            show-size-picker
            :page-sizes="[10, 20, 30, 40]"
          />
        </div>
      </n-spin>
    </n-card>

    <myDialog
      ref="myDialogRef"
      :data="tagData"
      :showModal="showModal"
      @closeModal="closeModal"
      @getTag="getTag"
    >
    </myDialog>
  </div>
</template>

<script setup>
import { ref, h, watch, inject, onMounted } from "vue";
import {
  AddCircleOutline,
  FlashOutline,
  SearchOutline,
} from "@vicons/ionicons5";
import { NTag, NButton, NPopconfirm, NPopover } from "naive-ui";
import myDialog from "./dialog.vue";
import { getAllTag, deleteTag } from "@/api/tag.ts";

const page = ref(1);
const limit = ref(10);
const loading = ref(false);
const tagData = ref([]);
const columns = ref([
   {
    type: "selection",
    fixed: 'left' 
  },
  {
    title: "名称",
    key: "t_name",
  },
  {
    title: "类型",
    key: "t_type",
  },
  {
    title: "描述",
    key: "t_description",
    render(row) {
      return [
        h(
          NPopover,
          {
            trigger: "hover",
          },
          {
            trigger: () => {
              return h(
                NTag,
                {
                  style: {
                    marginRight: "6px",
                  },
                  type: "success",
                  bordered: false,
                },
                {
                  default: () => "描述",
                }
              );
            },
            default: () => {
              return row.t_description;
            },
          }
        ),
      ];
    },
  },
  {
    title: "创建人",
    key: "t_creator",
  },
  {
    title: "状态",
    key: "t_status",
    render(row) {
      return h(
        NTag,
        {
          style: {
            marginRight: "6px",
          },
          type: row.t_status == 1 ? "success" : "error",
          bordered: false,
        },
        {
          default: () => (row.t_status == 1 ? "正常" : "禁用"),
        }
      );
    },
  },
  {
    title: "创建时间",
    key: "t_createTime",
    width: 200,
  },
  {
    title: "操作",
    key: "actions",
    fixed: "right",
    render(row) {
      return [
        h(
          NPopconfirm,
          {
            positiveButtonProps: {
              size: "tiny",
              // color: "#007293",
            },
            negativeButtonProps: {
              size: "tiny",
              // color: "#007291",
            },
            onPositiveClick: (e) => {
              // console.log(row);
              let params = {
                id: row.t_id,
              };
              deleteTag(row.t_id).then((res) => {
                if (res.code == 200) {
                  window.$message.success(res.message);

                  getTag();
                } else {
                  window.$message.error(res.message);
                }
              });
            },
          },
          {
            trigger: () => {
              return h(
                NButton,
                {
                  style: { marginRight: "10px" },
                  strong: true,
                  secondary: true,
                  type: "error",
                  size: "small",
                },
                { default: () => "删除" }
              );
            },
            default: () => {
              return "如果存在子级，会将子级删除？";
            },
          }
        ),
        h(
          NButton,
          {
            strong: true,
            secondary: true,
            type: "warning",
            size: "small",
            onClick: () => sendMail(row),
          },
          { default: () => "编辑" }
        ),
      ];
    },
    width: 150,
  },
]);
const showModal = ref(false);
const myDialogRef = ref(null);
const rowKey = (row) => row.t_id;
const tableHeight = ref(inject("tableHeight"));

onMounted(() => {
  getTag();
});
const getTag = () => {
  loading.value = true;
  getAllTag()
    .then((res) => {
      if (res.code == 200) {
        tagData.value = [];
        tagData.value = res.data;
      }

      console.log(res);
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
    });
};
const handleClick = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 1000);
};
const closeModal = () => {
  showModal.value = false;
};
const openDialog = () => {
  showModal.value = true;
  console.log(myDialogRef.value);
  myDialogRef.value.init();
};
</script>
